//
//  ItemCell.m
//  table test
//
//  Created by tao故乡的云 on 2017/4/10.
//  Copyright © 2017年 故乡的云. All rights reserved.
//

#import "ItemCell.h"



@interface ItemCell ()
@property (nonatomic,weak) UIButton *deleteBtn;
@property (nonatomic,weak) UIView *topView;
/// 手势起始坐标
@property (nonatomic,assign) CGFloat startX;
/// 右滑编辑状态
@property (nonatomic,getter=isOn) bool on;
/// 忽略此次cell点击事件标志
@property (nonatomic,getter=isFlag) bool flag;
/// 左滑删除编辑标志
@property (nonatomic,getter=isDeleteEditing) bool deleteEditing;
@end

@implementation ItemCell
CGFloat const kOffset = 80; // topView形变量

#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor lightGrayColor];
        // leftBtn
        UIButton *leftBtn = [[UIButton alloc] init];
        [self.contentView addSubview:leftBtn];
        self.leftBtn = leftBtn;
        leftBtn.titleLabel.textColor = [UIColor whiteColor];
        [leftBtn setTitle:@"已完成" forState:UIControlStateNormal];
        // deleteBtn
        UIButton *deleteBtn = [[UIButton alloc] init];
        [self.contentView addSubview:deleteBtn];
        self.deleteBtn = deleteBtn;
        deleteBtn.titleLabel.textColor = [UIColor whiteColor];
        [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        [deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
#warning 有scrollView时事件不能传递到tableView，cell不能选中； 去掉scrollView，能选中，但是拖动手势不能传递给父视图，且选中时会导致新添加的子视图透明化；
        // topView
        UIView *topView = [[UIView alloc] init];
        self.topView = topView;
        [self.contentView addSubview:topView];
        topView.backgroundColor = [UIColor whiteColor];
        [topView addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]];
        [topView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]];
//       . [topView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]];
        
        // textLabel
        UILabel *textLabel = [[UILabel alloc] init];
        self.titleLabel = textLabel;
        [self.topView addSubview:textLabel];
    }
    
    return self;
}

#pragma mark - 属性设置
- (void)setOn:(bool)on {
    _on = on;
    // 右滑编辑处理
    if ([self.delegate respondsToSelector:@selector(itemCell:didOn:)]) {
        [self.delegate itemCell:self didOn:on];
    }
}

- (void)setDeleteEditing:(bool)deleteEditing {
    _deleteEditing = deleteEditing;
    // 右滑编辑处理
    if ([self.delegate respondsToSelector:@selector(itemCell:didShowDelete:)]) {
        [self.delegate itemCell:self didShowDelete:deleteEditing];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if (selected && self.isFlag) { // 忽略此次cell点击事件
        self.flag = NO;
        return;
    }
    [super setSelected:selected animated:animated];
    
    if (selected) {
        self.topView.backgroundColor = [UIColor lightGrayColor];
    }else {
        self.topView.backgroundColor = [UIColor whiteColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    if ([self.delegate respondsToSelector:@selector(itemCellWillTouch:)]) {
        [self.delegate itemCellWillTouch:self];
    }
    
    if (self.isDeleteEditing) {
        [UIView animateWithDuration:0.5 animations:^{
            _topView.transform = CGAffineTransformMakeTranslation(0, 0);
        }];
        self.deleteEditing = NO;
        self.flag = YES; // 忽略此次cell点击事件
        return;
    }
    
    [super setHighlighted:highlighted animated:animated];
    
    if (self.isSelected) {
        return;
    }
    if (highlighted) {
        self.topView.backgroundColor = [UIColor lightGrayColor];
    }else {
        self.topView.backgroundColor = [UIColor whiteColor];
    }
    
}

#warning   ...BUG...
/// 在cell聚焦时（被点击），告诉上一个cell失去焦点，结束编辑状态
- (void)whenDidTouchEndEditOfPreviousCell {
    if (self.isDeleteEditing) {
        [UIView animateWithDuration:0.5 animations:^{
            _topView.transform = CGAffineTransformMakeTranslation(0, 0);
        }];
        self.deleteEditing = NO;
    }
}

#pragma mark - 事件处理
- (void)deleteBtnClick:(UIButton *)btn {
#warning   ...
    
    if ([_delegate respondsToSelector:@selector(itemCell:didDeleteBtnClick:)]) {
        [self.delegate itemCell:self didDeleteBtnClick:btn];
    }
    
}

///
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
#warning   ... 启动table view移动功能
    if ([_delegate respondsToSelector:@selector(itemCell:didLongPress:)]) {
        [self.delegate itemCell:self didLongPress:longPress];
    }
    if ([self.delegate respondsToSelector:@selector(itemCellWillTouch:)]) {
        [self.delegate itemCellWillTouch:self];
    }
}

/// 左右滑动
- (void)pan:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateBegan) {
        _startX = [pan locationInView:self].x;
        if ([self.delegate respondsToSelector:@selector(itemCellWillTouch:)]) {
            [self.delegate itemCellWillTouch:self];
        }
    }else if (pan.state == UIGestureRecognizerStateChanged) {
        CGFloat x = [pan locationInView:self].x;
        CGFloat offsetX = x - _startX;
        
        if (self.isDeleteEditing ) { // 正在左滑删除状态中
            if (offsetX > 0 && offsetX <= kOffset) {
                CGAffineTransform t = CGAffineTransformMakeTranslation(-kOffset, 0);
                _topView.transform = CGAffineTransformTranslate(t, offsetX, 0);
            }
            return;
        }
        if (offsetX < 0) { // 左滑
            offsetX = -offsetX >= kOffset ? -kOffset : offsetX;
            _topView.transform = CGAffineTransformMakeTranslation(offsetX, 0);
            self.contentView.backgroundColor = [UIColor redColor];
        }else{ // 右滑
            
            if(offsetX < kOffset){
                self.contentView.backgroundColor = [UIColor grayColor];
                _topView.transform = CGAffineTransformMakeTranslation(offsetX, 0);
                _leftBtn.transform = CGAffineTransformMakeScale(offsetX / kOffset * 0.1 + 1, offsetX / kOffset * 0.1 + 1);
                self.on = NO;
            }else {
                _topView.transform = CGAffineTransformMakeTranslation(kOffset + offsetX / kOffset, 0);
                _leftBtn.transform = CGAffineTransformMakeScale(1.1, 1.1);
                self.contentView.backgroundColor = [UIColor greenColor];
                self.on = YES;
            }
        }
    }else if (pan.state == UIGestureRecognizerStateEnded){
        CGFloat x = [pan locationInView:self].x;
        CGFloat offsetX = x - _startX;
        if (self.isDeleteEditing) { // 正在左滑删除状态中
            if (offsetX > 0 && offsetX < kOffset) {
                self.deleteEditing = offsetX > 0.5 * kOffset ? NO : YES;
                offsetX = offsetX > 0.5 * kOffset ? kOffset - offsetX : -offsetX;
                [UIView animateWithDuration:0.15 animations:^{
                _topView.transform = CGAffineTransformTranslate(_topView.transform, offsetX, 0);
                }];
            }else if (offsetX >= kOffset){
                [UIView animateWithDuration:0.1 animations:^{
                _topView.transform = CGAffineTransformMakeTranslation(0, 0);
                }];
                self.deleteEditing = NO;
            }
            return;
        }
        if (offsetX < 0) { // 左滑
            offsetX = -offsetX > 0.5 * kOffset ? -kOffset : 0;
            [UIView animateWithDuration:0.15 animations:^{
            _topView.transform = CGAffineTransformMakeTranslation(offsetX, 0);
            }];
            self.deleteEditing = -offsetX > 0.5 * kOffset ? YES :NO;
        }else{ // 右滑
        
            [UIView animateWithDuration:0.4 animations:^{
                _topView.transform = CGAffineTransformMakeTranslation(0, 0);
                _leftBtn.transform = CGAffineTransformMakeScale(1.0, 1.0);
            }];
        }
    }
}

#pragma mark - 布局子控件
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat offset = kOffset;
    CGFloat W = self.contentView.bounds.size.width;
    CGFloat H = self.contentView.bounds.size.height;
    
    self.leftBtn.frame = CGRectMake(0, 0, offset, H);
    self.deleteBtn.frame = CGRectMake(W - offset, 0, offset, H);
    self.topView.frame = CGRectMake(0, 0, W, H);
    self.titleLabel.frame = CGRectMake(15, 0, W - 30, H);
}
@end
